/**
 * 
 */
package org.gov.vis.data.controller;

import java.util.List;

import org.gov.vis.common.exceptions.InvalidInputException;
import org.gov.vis.common.exceptions.SystemException;
import org.gov.vis.data.dao.DistributionSemenDao;
import org.gov.vis.data.dao.DistributionSemenDaoImpl;
import org.gov.vis.data.dto.DistributionSemenDto;
import org.gov.vis.util.DBConstants;
import org.gov.vis.util.DU;
import org.gov.vis.util.GuiConstants;
import org.gov.vis.util.ValidatorUtils;

/**
 * @author user
 *
 */
public class DistributionSemenCtlrImpl implements DistributionSemenCtlr {

	/**
	 * Method called to Add or Edit the DistributionSemen
	 * @param prSemenDto
	 * @throws Exception
	 */

	public void addOrEditDisSemen(final DistributionSemenDto _disSemenDto) throws Exception {
		try{
			/*
			 * Below set of validations check for Required/Mandatory fields
			 */
			String _disSemenCodeErr = ValidatorUtils.validateString(rb.getString("dialog_label_dist_semen_code"), _disSemenDto.getCode(), 1, 20, true);
			if(_disSemenCodeErr != null)
				errorMsgs.put(rb.getString("dialog_label_dist_semen_code").toUpperCase(), _disSemenCodeErr);

			String _disSemenBullNoErr = ValidatorUtils.validateString(rb.getString("dialog_label_dist_semen_bullno"), _disSemenDto.getBullNo(), 1, 50, true);
			if(_disSemenBullNoErr != null)
				errorMsgs.put(rb.getString("dialog_label_dist_semen_bullno").toUpperCase(), _disSemenBullNoErr);
			
			/*
			 * Below are NOT required fields but are validated on their data type 
			 */
			String _disSemenNoOfStrawsErr = ValidatorUtils.validateOnlyNumber(rb.getString("dialog_label_dist_semen_nostrwas"), _disSemenDto.getNoStrawas(), 0, Short.MAX_VALUE, false, false);
			if(_disSemenNoOfStrawsErr != null)
				errorMsgs.put(rb.getString("dialog_label_dist_semen_nostrwas").toUpperCase(), _disSemenNoOfStrawsErr);
			
			String _disSemenDODistrErr = ValidatorUtils.validateString(rb.getString("dialog_label_dist_semen_dtofdistr"), DU.format(_disSemenDto.getDoDstrbut(), DBConstants.DISPLAY_DATE_FORMAT), 6, 50, true);
			if(_disSemenDODistrErr != null)
				errorMsgs.put(rb.getString("dialog_label_dist_semen_dtofdistr").toUpperCase(), _disSemenDODistrErr);


			if (errorMsgs != null && !errorMsgs.isEmpty())
				throw new InvalidInputException();

			/*
			 * Below set of validations check for duplicate records
			 */
			//check if there exists anDisSemen DisSemen Object with the same Code
			//If Code has been duplicated, then throw the alert
			DistributionSemenDto DisSemenDto = null;
			if(_disSemenDto.getDisSemenId() > 0){
				//get the DisSemenDto on the passed Code name
				DisSemenDto = this.getDisSemenOnCode(_disSemenDto.getCode());
				if(DisSemenDto != null && DisSemenDto.getDisSemenId() != _disSemenDto.getDisSemenId()){
					errorMsgs.put(rb.getString("dialog_label_dist_semen_code").toUpperCase(), rb.getString(GuiConstants.DATA_DUPLICATE_DISTRIBUTIONSEMEN_MSG));
				}
			}//end if
			else{
				//get the DisSemenDto on the passed Code name
				DisSemenDto = this.getDisSemenOnCode(_disSemenDto.getCode());
				if(DisSemenDto != null){
					errorMsgs.put(rb.getString("dialog_label_dist_semen_code").toUpperCase(), rb.getString(GuiConstants.DATA_DUPLICATE_DISTRIBUTIONSEMEN_MSG));
				}
			}//end else

			if (errorMsgs != null && !errorMsgs.isEmpty())
				throw new InvalidInputException();

			final DistributionSemenDao _prSemenDao = new DistributionSemenDaoImpl();

			//add or edit the Distribution Semen Object
			_prSemenDao.addOrEditDistributionSemen(_disSemenDto);
		}
		catch(InvalidInputException ie){
			ie.printStackTrace();
			throw ie;
		}
		catch (Exception ex) {
			ex.printStackTrace();
			if(_disSemenDto.getDisSemenId() > 0){
				throw new SystemException(rb.getString(GuiConstants.DATA_EDIT_DISTRBUTIONSEMEN_EXCEPTION));
			}
			else{
				throw new SystemException(rb.getString(GuiConstants.DATA_ADD_DISTRBUTIONSEMEN_EXCEPTION));
			}
		}
	}//end addOrEditDisSemen

	/**
	 * Method called to delete the DistributionSemen Object based on the passed disSemen Id
	 * @param _disSemenId
	 * @throws Exception
	 */

	public void deleteDisSemen(final long _disSemenId) throws Exception {
		try{
			final DistributionSemenDao _prSemenDao = new DistributionSemenDaoImpl();
			_prSemenDao.deleteDistributionSemen(_disSemenId);
		}
		catch (Exception ex) {
			throw new SystemException(rb.getString(GuiConstants.DATA_DELETE_DISTRBUTIONSEMEN_EXCEPTION));
		}
	}//end deleteDisSemen

	/**
	 * Method called to get the DistributionSemen based on the passed primary key Id
	 * @param _disSemenId
	 * @return
	 * @throws Exception
	 */

	public DistributionSemenDto getDisSemen(final long _disSemenId) throws Exception {
		try{
			final DistributionSemenDao _prSemenDao = new DistributionSemenDaoImpl();
			DistributionSemenDto _DisSemenDto = new DistributionSemenDto();
			_DisSemenDto.setDisSemenId(_disSemenId);

			DistributionSemenDto prSemenDto = null;
			List<DistributionSemenDto> _prSemenList = _prSemenDao.getDistributionSemens(_DisSemenDto);
			if(_prSemenList != null && !_prSemenList.isEmpty()){
				prSemenDto = _prSemenList.get(0);
			}

			return prSemenDto;
		}
		catch (Exception ex) {
			throw new SystemException(rb.getString(GuiConstants.DATA_VIEW_ADD_DISTRBUTIONLN_EXCEPTION));
		}
	}//end getDisSemen


	/**
	 * Method called to get the DistributionSemen based on the passed Breed
	 * @param _disSemenCode
	 * @return
	 * @throws Exception
	 */

	public DistributionSemenDto getDisSemenOnBreed(final String _breed) throws Exception {
		final DistributionSemenDao _disSemenDao = new DistributionSemenDaoImpl();
		DistributionSemenDto _DisSemenDto = new DistributionSemenDto();
		_DisSemenDto.setBreed(_breed);

		DistributionSemenDto disSemenDto = null;
		List<DistributionSemenDto> _prSemenList = _disSemenDao.getDistributionSemens(_DisSemenDto);
		if(_prSemenList != null && !_prSemenList.isEmpty()){
			disSemenDto = _prSemenList.get(0);
		}

		return disSemenDto;
	}//end getDisSemenOnBreed

	/**
	 * Method called to get the DistributionSemen based on the passed BullNo
	 * @param _prSemenCode
	 * @return
	 * @throws Exception
	 */

	public DistributionSemenDto getDisSemenOnBullNo(final String _bullNo) throws Exception {
		final DistributionSemenDao _disSemenDao = new DistributionSemenDaoImpl();
		DistributionSemenDto _DisSemenDto = new DistributionSemenDto();
		_DisSemenDto.setBullNo(_bullNo);

		DistributionSemenDto disSemenDto = null;
		List<DistributionSemenDto> _disSemenList = _disSemenDao.getDistributionSemens(_DisSemenDto);
		if(_disSemenList != null && !_disSemenList.isEmpty()){
			disSemenDto = _disSemenList.get(0);
		}

		return disSemenDto;
	}//end getDisSemenOnBullNo

	/**
	 * Method called to get the DistributionSemen based on the passed DisSemen Object
	 * @param prSemenDto
	 * @return
	 * @throws Exception
	 */

	public DistributionSemenDto getDisSemen(final DistributionSemenDto _DisSemenDto) throws Exception {
		final DistributionSemenDao _disSemenDao = new DistributionSemenDaoImpl();

		DistributionSemenDto disSemenDto = null;
		List<DistributionSemenDto> _disSemenList = _disSemenDao.getDistributionSemens(_DisSemenDto);
		if(_disSemenList != null && !_disSemenList.isEmpty()){
			disSemenDto = _disSemenList.get(0);
		}

		return disSemenDto;
	}//end getDisSemens

	/**
	 * Method called to get the list of DistributionSemen based on the passed DisSemen Object
	 * @param _DisSemenDto
	 * @return
	 * @throws Exception
	 */

	public List<DistributionSemenDto> getDisSemens(final DistributionSemenDto _DisSemenDto) throws Exception {
		final DistributionSemenDao _disSemenDao = new DistributionSemenDaoImpl();
		return _disSemenDao.getDistributionSemens(_DisSemenDto);
	}//end getDisSemens

	/**
	 * Method called to search for the DistributionSemens based on the passed DisSemen Object
	 * @param prSemenDto
	 * @return
	 * @throws Exception
	 */

	public List<DistributionSemenDto> searchDisSemens(DistributionSemenDto _DisSemenDto) throws Exception {
		try{
			final DistributionSemenDao _disSemenDao = new DistributionSemenDaoImpl();
			return _disSemenDao.searchDistributionSemens(_DisSemenDto);
		}
		catch (Exception ex) {
			throw new SystemException(rb.getString(GuiConstants.DATA_VIEW_MANTAN_DISTRBUTIONSEMEN_EXCEPTION));
		}
	}//end searchDisSemens

	/**
	 * Method called to get the DistributionSemen based on the passed Code
	 * @param _disCode
	 * @return
	 * @throws Exception
	 */

	public DistributionSemenDto getDisSemenOnCode(final String _disCode) throws Exception {
		final DistributionSemenDao _disSemenDao = new DistributionSemenDaoImpl();
		DistributionSemenDto _disSemenDto = new DistributionSemenDto();
		_disSemenDto.setCode(_disCode);

		DistributionSemenDto disSemenDto = null;
		List<DistributionSemenDto> _disSemenList = _disSemenDao.getDistributionSemens(_disSemenDto);
		if(_disSemenList != null && !_disSemenList.isEmpty()){
			disSemenDto = _disSemenList.get(0);
		}

		return disSemenDto;
	}//end getAIOnCode

}//end class
